import java.util.Arrays;

public class MyarrayList implements IList{

    public int[]array;
    public int useside;
    public static final int l = 5;

    public  MyarrayList(){
      array = new int[l];
    }

    //检查是否放满了
     @Override
    public boolean isFull() {
        if(useside == array.length){
            return true;
        }
        return false;
    }

    @Override
    public boolean isEmpty() {
        if(useside == 0){
            return true;
        }
        return false;
    }

    //自己设置的2倍扩容
    public void grow(){
        array = Arrays.copyOf(array,2 * array.length);
    }

    //在顺序表中，默认在最后位置添加元素，这个没有
    @Override
    public void add(int data) {

        array[useside++] = data;
    }

    public void checkpos(int pos){
        if(pos < 0 || pos > useside){//这里在数组末尾插入是合法的
            throw new ArrayIndexOutOfBoundsException("pos位置不合法" + pos);
        }                            //自定义异常
    }

    public void checkpos2(int pos){ //一个典型的受检异常
        if(pos < 0 || pos >= useside){
            throw new ListEmptyException("获取位置不合法" + pos);
        }
    }

    @Override
    public void add(int pos,int data) {
        checkpos(pos);
        if(isFull()){
            grow();
        }                        //移动元素
     for(int i = useside - 1;i >= pos;i--){
         array[i+1] = array[i];
     }
         array[pos] = data;
         useside++;             //新加入了元素就更新计数
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < useside; i++) {//这里写成了array.length会打印没用的0
            if (array[i] == toFind){
                return i;
            }
        }
        return -1;             //-1肯定不属于下标
    }

    @Override
    public int get(int pos) {
        if(isEmpty()){
            throw new ArrayIndexOutOfBoundsException("数组是空的");
        }
        checkpos2(pos);

        return array[pos];
    }

    @Override
    public void set(int pos, int value) {
        checkpos(pos);
        array[pos] = value;
    }



    @Override
    public int size() {
        return array.length;
    }

    @Override
    public void clear() {
//        for (int i = 0; i < array.length; i++) {
//            array[i] = 0/Null;
//        }
        useside = 0;
    }

    @Override
    public void display() {//这里也是一样，要写useside而不是array.length
        for (int i = 0; i < useside; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    @Override
    public void remove(int toRemove) {
        int v = indexOf(toRemove);
        if(v == -1){
            System.out.println("找不到要删除的值");
            return;             //终止代码
        }
        for (int i = v; i < useside-1; i++) {
            array[i] = array[i+1];
        }
        useside--;
    }

    public static void main(String[] args) {

        MyarrayList x = new MyarrayList();


        x.add(0,9);       //在1位置添加一个9
        x.add(1,2);
        x.add(2,3);
        x.display();
//        try {
//            x.checkpos(100);
//        }catch (ArrayIndexOutOfBoundsException e){
//            e.printStackTrace();
//        }

        int c = x.get(1);
        System.out.println(c);

        x.remove(100);
        x.display();

    }

}
